package com.ui.basic.service.masterdata;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.basic.util.Globals;
import com.ui.basic.assembler.masterdata.CountryAssembler;
import com.ui.basic.command.masterdata.CommandCountry;
import com.ui.basic.ibatis.dao.masterdata.TblMCountryDAO;
import com.ui.basic.ibatis.model.masterdata.TblMCountry;
import com.ui.basic.ibatis.model.masterdata.TblMCountryExample;

@Service("countryService")
public class CountryServiceImpl implements CountryService {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(CountryServiceImpl.class);

	@Autowired
	private TblMCountryDAO tblMCountryDAO;
	
	public void setTblMCountryDAO(TblMCountryDAO tblMCountryDAO) {
		this.tblMCountryDAO = tblMCountryDAO;
	}

	@Transactional(readOnly = true)
	public CommandCountry getCountry(CommandCountry commandCountry)
			throws SQLException {
		LOGGER.debug(">>>CountryServiceImpl.getCountry");
		if (commandCountry != null) {
			TblMCountryExample example = new TblMCountryExample();

			int rec = 0;
			try {
				rec = tblMCountryDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}
			commandCountry.calculate(rec);
			example.setIntRowNum((commandCountry.getPgIndex() - 1)
					* commandCountry.getPgRows());
			example.setIntRowsPerPage(commandCountry.getPgRows());
			example.setOrderByClause("CODE");
			commandCountry.setData(tblMCountryDAO.selectByExample(example));
		}
		return commandCountry;
	}

	@Transactional(readOnly = true)
	public CommandCountry getCountryDetail(CommandCountry commandCountry)
			throws SQLException {
		LOGGER.debug(">>>CountryServiceImpl.getCountryDetail");
		commandCountry.setProperty(tblMCountryDAO.selectByPrimaryKey(commandCountry.getId()));
		return commandCountry;
	}

	@Transactional
	public void save(CommandCountry commandCountry) throws SQLException {
		LOGGER.debug(">>>CountryServiceImpl.save");
		CountryAssembler assembler = new CountryAssembler();
		TblMCountry country = assembler.toEntity(commandCountry);

		if(commandCountry.getId() != null){
			country.setChangedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			country.setChangeddate(new Date());
			try {
				tblMCountryDAO.updateByPrimaryKeySelective(country);

			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}else{
			country.setCreatedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			country.setCreateddate(new Date());
			try {
				tblMCountryDAO.insert(country);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}
	}

	@Transactional
	public void delete(CommandCountry commandCountry) throws SQLException {
		LOGGER.debug(">>>CountryServiceImpl.save");
		TblMCountryExample example = new TblMCountryExample();
		
		if(commandCountry.getId()!= null){
			example.createCriteria().andIdEqualTo(commandCountry.getId());
		}else if(commandCountry.getSelectedRecord()!=null && commandCountry.getSelectedRecord().length>0){
			List<Long> idList = new ArrayList<Long>();
			for(String strId: commandCountry.getSelectedRecord())
				idList.add(Long.valueOf(strId));
			example.createCriteria().andIdIn(idList);
		}
		
		tblMCountryDAO.deleteByExample(example);			
	}
}
